using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.ComponentModel;
using System.Reflection;

namespace JVIT.TimeSheet.BLL
{
    public class WinampControl
    {
        #region DLL Imports
        // Get a handle to an application window.
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        // Activate an application window.
        [DllImport("USER32.DLL")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        #endregion

        #region public controls
        public static void Previous()
        {
            IssueCommand(WinampCommand.Previous);
        }
        public static void Play()
        {
            IssueCommand(WinampCommand.Play);
        }
        public static void Pause()
        {
            IssueCommand(WinampCommand.Pause);
        }
        public static void Stop()
        {
            IssueCommand(WinampCommand.Stop);
        }
        public static void Next()
        {
            IssueCommand(WinampCommand.Next);
        }
        #endregion

        #region logics implementation
        private static void IssueCommand(WinampCommand command)
        {
            IntPtr appHandle = FindWindow("Winamp v1.x", null);
            if (appHandle != IntPtr.Zero)
            {
                SetForegroundWindow(appHandle);
                SendKeys.Send(GetValue(command));
            }
        }
        #endregion

        #region helper methods
        private static String GetValue(Enum e)
        {

            FieldInfo fieldInfo = e.GetType().GetField(e.ToString());
            DescriptionAttribute[] enumAttributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (enumAttributes.Length > 0)
            {
                return enumAttributes[0].Description;
            }
            return e.ToString();
        }
        #endregion
    }
    enum WinampCommand
    {
        [Description("z")]
        Previous,
        [Description("x")]
        Play,
        [Description("c")]
        Pause,
        [Description("v")]
        Stop,
        [Description("b")]
        Next,
    }
}
